﻿namespace CRM.DataAccess.DAOs
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using CRM.DataAccess.Entities;
    using System.Linq;
    using System.Data.Entity.Validation;
    using System.Diagnostics;
    using System;

    public class TermsAndConditionsDAO : DbContext
    {
        public TermsAndConditionsDAO()
            : base("name=Entities")
        {
        }
        public DbSet<TERMS_AND_CONDITIONS> TERMS_AND_CONDITIONS { get; set; }

        //Get TERMS_AND_CONDITIONS
        public List<TERMS_AND_CONDITIONS> GetAllTAC()
        {
            try
            {
                var tacQuery = from tac in TERMS_AND_CONDITIONS
                               select tac;
                List<TERMS_AND_CONDITIONS> tacs = tacQuery.ToList();
                if (tacs.Count > 0)
                    return tacs;
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Check exist TERMS_AND_CONDITIONS
        public int CheckExist(string TACName)
        {
            try
            {
                var Query = from tac in TERMS_AND_CONDITIONS
                            where tac.NAME.ToUpper() == TACName.ToUpper()
                            select tac;
                List<TERMS_AND_CONDITIONS> tacs = Query.ToList();
                if (tacs.Count > 0)
                    return 1;
                return 0;
            }
            catch
            {
                return -1;
            }
        }

        //Add new TERMS_AND_CONDITIONS
        public int AddNewTAC(TERMS_AND_CONDITIONS tac)
        {
            try
            {
                this.TERMS_AND_CONDITIONS.Add(tac);
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        //Get TERMS_AND_CONDITIONS by Condition
        public List<TERMS_AND_CONDITIONS> GetTACByCondition(DateTime? dateFrom, DateTime? dateTo, string TACName)
        {
            try
            {
                var Query = from tac in TERMS_AND_CONDITIONS
                            where ((TACName != "" ? (tac.NAME.ToUpper().Contains(TACName.ToUpper())) : (true))
                            //(tac.NAME.ToUpper() == (TACName == "" ? tac.NAME.ToUpper() : TACName.ToUpper())
                           && (tac.DATE_CREATED >= dateFrom && tac.DATE_CREATED < dateTo))
                            select tac;
                List<TERMS_AND_CONDITIONS> tacs = Query.AsNoTracking().ToList();
                if (tacs.Count > 0)
                    return tacs;
                return null;
            }
            catch
            {
                return null;
            }
        }

        //Delete TERMS_AND_CONDITIONS
        public int DeleteTAC(long ID)
        {
            try
            {
                TERMS_AND_CONDITIONS.RemoveRange(TERMS_AND_CONDITIONS.Where(x => x.ID == ID));
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }

        //Get TERMS_AND_CONDITIONS by ID
        public TERMS_AND_CONDITIONS GetTACByID(long ID)
        {
            try
            {
                var Query = from tac in TERMS_AND_CONDITIONS
                            where tac.ID == ID
                            select tac;
                List<TERMS_AND_CONDITIONS> tacs = Query.ToList();
                if (tacs.Count > 0)
                    return tacs[0];
                return null;
            }
            catch
            {
                return null;
            }
        }
        //update TERMS_AND_CONDITIONS
        public int UpdateTAC(TERMS_AND_CONDITIONS tac)
        {
            try
            {
                var original = TERMS_AND_CONDITIONS.Find(tac.ID);
                if (original != null)
                {
                    Entry(original).CurrentValues.SetValues(tac);
                    SaveChanges();
                    return 1;
                }
                return -1;
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
                return -1;
            }
        }

    }
}
